package com.guigu.qingcheng.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.guigu.qingcheng.domain.entity.PageResult;
import com.guigu.qingcheng.domain.pojo.TbAlbum;
import com.guigu.qingcheng.domain.pojo.TbBrand;
import com.guigu.qingcheng.mapper.TbAlbumMapper;
import com.guigu.qingcheng.service.TbAlbumService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.guigu.qingcheng.utils.CommonResult;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author testjava
 * @since 2020-07-05
 */
@Service
public class TbAlbumServiceImpl extends ServiceImpl<TbAlbumMapper, TbAlbum> implements TbAlbumService {

    @Override
    public CommonResult findPage(Map<String, Object> searchMap, Integer page, Integer size) {
        QueryWrapper<TbAlbum> wrapper = createExample(searchMap);
        //创建page对象
        Page<TbAlbum> pageAlbum = new Page<>(page,size);
        baseMapper.selectPage(pageAlbum,null);
        if (pageAlbum.getRecords() == null && pageAlbum.getTotal() == 0){
            return CommonResult.error();
        }
        return CommonResult.ok().data("albumRows",pageAlbum.getRecords()).data("total",pageAlbum.getTotal());
    }


    // 根据查询条件封装模块对象
    private QueryWrapper<TbAlbum> createExample(Map<String, Object> searchMap) {
        QueryWrapper<TbAlbum> wrapper = new QueryWrapper<>();
        if(searchMap!=null) {
            if(searchMap.get("id")!=null)
                wrapper.eq("id",searchMap.get("id"));
            if(searchMap.get("title")!=null && !"".equals(searchMap.get("title")))
                wrapper.like("title",searchMap.get("title"));
        }
        return wrapper;
    }
}
